iT邦幫忙

2024 iThome 鐵人賽

DAY 29
1

想要讓chat gpt產生出我們想要的結果,我們必須寫好提示慈(prompt),本篇文章就來寫一個基於CO-STAR架構所寫的prompt


CO-STAR

CO-STAR是一個用於規範prompt的架構,主要分為:

  • Context(上下文):提供所需達成目標的背景
  • Objective(目標):定義所需達成的目標
  • Style(風格):定義回應的風格
  • Tone(語氣):定義回應的語氣
  • Audience(受眾):提供回應的受眾
  • Response(回應):定義回應所需的格式

move_prompt

有了寫作的基礎,我們就可以試著寫出符合我們目標要求的提示詞了,當然也可以使用網路上的提示詞產生器,各位也可以依照自己的要求尋找適合的提示詞產生器來協助寫作

以下提供各位如何讓gpt生成國際象棋走法的範例做為參考:

<string name="move_prompt">
"
# CONTEXT #
Your task is to generate the best move strategy for black pieces in a chess game, following the basic rules of chess, based on the current board situation, considering famous chess games, and from the perspective of a grandmaster. You are playing as black, and you can only move black pieces on the board. The move must follow the Sicilian Defense opening, and you must generate exactly one move without explanation.

Notation explanation:
The board data format is [coordinate: piece].
Only pieces starting with 'b' can be moved. Black pieces can only capture white pieces or move to empty squares.
If the first letters of the starting and ending coordinates are the same, it represents a vertical move. If the second numbers of the starting and ending coordinates are the same, it represents a horizontal move.
The first letter 'b' denotes black pieces.
The first letter 'w' denotes white pieces.
The second letter 'K' represents the king.
The second letter 'Q' represents the queen.
The second letter 'R' represents the rook.
The second letter 'N' represents the knight.
The second letter 'B' represents the bishop.
The second letter 'P' represents the pawn.
If the piece code at a coordinate is two spaces, it represents an empty square.

Basic rules:
Rooks can move vertically or horizontally if all squares between the start and end positions are empty. The end square should be empty or occupied by a white piece.
Bishops can move diagonally if all squares between the start and end positions are empty. The end square should be empty or occupied by a white piece.
Queens can move vertically, horizontally, or diagonally if all squares between the start and end positions are empty. The end square should be empty or occupied by a white piece.
Kings can move one square in any direction.
Knights can move in an L-shape: two squares in one direction and then one square perpendicular.
Pawns can move forward one square, or two squares on their first move. They cannot move backward.
All captures follow the piece's movement rules, except pawns, which capture diagonally.

# OBJECTIVE #
Analyze the input board data and output the best move strategy in one step. The ultimate goal is to capture the white king.

# STYLE #
The result should be formatted as a move strategy in four characters:
starting coordinate + ending coordinate
Example: c2c3

# TONE #
Use a formal and concise tone.

# AUDIENCE #
The target audience is professionals.

# RESPONSE #
Do not provide any explanation.
    </string>

我們將這段文字放在String中,請求API時再調用

GPTMove

回到我們已經實作的GPTMove,可以發現在建構元中,我們其實已經呼叫setPrompt(),也就是初始化system類別的prompt了,之後每次呼叫setGPT()時,會傳入user類別的data(一個hashmap型態的棋盤資料)去做API請求,讓gpt根據prompt的內容,生成出最好的一步棋

public void setPrompt(){
        messages.add(new GPTApiRequest.ChatMessage("system", this.activity.getString(R.string.move_prompt)));
    }
public void setGPT(){
        GPTApiRequest request = new GPTApiRequest();
        messages.add(new GPTApiRequest.ChatMessage("user", chessboardData.toString()));
        request.setMessages(messages);

        gptApiService.getChatGPTRespond(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<GPTApiRespond>>() {
                    ......
                });
    }

至此,我們就已經實作完所有人機對弈的功能,當使用者選擇人機對弈進入對局,就會根據使用者下棋的盤面送給gpt請求 API,返回gpt生成的結果顯示在面上,實現人機對弈的功能

成果

經過實測,我們會發現其實效果不能說是不好,只能說非常差,經常會發生第一步就不照規則行走,或者棋子憑空消失,或是到第二、三步時就違反行走規則甚至移動對手棋子的情況發生,本人也多次嘗試修改提示詞,但結果都不如人意......

推測可能是因為chat gpt並非專門訓練用於對弈的模型,本身有巨量與其不相關的資料,且使用API方式請求的結果與直接使用網頁對話結果亦有明顯差異,才造成這些結果


結論

結論:

  • 使用API串接gpt生成國際象棋行走步數的效果不如人意,請斟酌使用
  • 人機對弈測試成果僅供參考,不建議直接使用,也不用期望
    它會像某深藍一樣,別想了(

上一篇
【DAY 28】chatgpt - API Client及Service
下一篇
【DAY 30】完賽感言+總結
系列文
基於Firebase整合生成式AI研究開發雙人國際象棋系統(Based on Firebase and AI to research chess system)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言